package com.androider.timememory.activity;

import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.R;
import com.androider.timememory.adapter.RecordAdapter;
import com.androider.timememory.db.RecordModel;
import com.androider.timememory.provider.SearchSuggestionProvider;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class SearchResults extends Activity {
	private Context mContext;
	private ListView listView;
	private TextView noResults;
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_results_layout);
		mContext = this;
		
		listView = (ListView) findViewById(R.id.search_results_list);
		noResults = (TextView) findViewById(R.id.no_results);
		title = (TextView) findViewById(R.id.title);
		title.setText(R.string.title_search_results);
		
		findViewById(R.id.search).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onSearchRequested();
			}
			
		});
		
		final Intent intent = getIntent();
		final String queryAction = intent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)) {
            doSearchQuery(intent);
        }
	}
	
	/** 
     * Called when new intent is delivered.
     *
     * This is where we check the incoming intent for a query string.
     * 
     * @param newIntent The intent used to restart this activity
     */
    @Override
    public void onNewIntent(final Intent newIntent) {
        super.onNewIntent(newIntent);
        
        // get and process search query here
        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)) {
            doSearchQuery(queryIntent);
        }
    }
    
    /**
     * Search all the notes by the column "note".
     * @param queryIntent
     * @return
     */
	private void doSearchQuery(final Intent queryIntent) {
		final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
		
		// Record the query string in the recent queries suggestions provider.
        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, 
                SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
        suggestions.saveRecentQuery(queryString, null);
        
		ArrayList<HashMap<String, Object>> list = RecordModel.searchByNote(mContext, queryString);
		if(list != null && list.size() > 0) {
			RecordAdapter adapter = new RecordAdapter(mContext, list);
			listView.setAdapter(adapter);
			listView.setVisibility(View.VISIBLE);
			noResults.setVisibility(View.GONE);
		} else {
			listView.setVisibility(View.GONE);
			noResults.setVisibility(View.VISIBLE);
		}
	}
}
